import {
    RouteRecordRaw,
    Router,
    RouteLocationNormalizedGeneric,
    RouteLocationNormalizedLoadedGeneric,
    NavigationGuardNext,
    createRouter,
    createWebHistory
} from 'vue-router';
import Home from '@/pages/home/index.vue';
import Wagmi from '@/pages/wagmi_connect/index.vue';

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        meta: {
            requiresAuth: true
        }
    },
    {
        path: '/wallet',
        name: 'Wallet',
        meta: {
            requiresAuth: true
        },
        children: [
            {
                path: 'wagmi',
                name: 'Wagmi',
                component: Wagmi,
                meta: { requiresAuth: true },
            }
        ]
    },
];

const router:Router = createRouter({
    history: createWebHistory(),
    routes
});
// 路由跳转之后执行的函数
router.afterEach((to: RouteLocationNormalizedGeneric, from: RouteLocationNormalizedLoadedGeneric) => {
    // 获取跳转到的路由的路径的层级
    const toDepth = to.path.split('/').length
    // 获取跳转前的路由的路径的层级
    const fromDepth = from.path.split('/').length
    // 判断跳转后的路由的路径的层级是否比跳转前的路由的路径的层级小
    to.meta.transition = toDepth < fromDepth ? 'slide-right' : 'slide-left'
})

// router/index.js 或 router/index.ts
router.beforeEach((to: RouteLocationNormalizedGeneric, _from: RouteLocationNormalizedLoadedGeneric, next: NavigationGuardNext) => {
    if (to.matched.some(record => record.meta.requiresAuth)) {
        // 检查用户是否已登录
        const isAuthenticated: boolean = true;
        if (!isAuthenticated) {
            next('/login'); // 重定向到登录页面
        } else {
            next();
        }
    } else {
        next();
    }
});

export default router;
